package org.zhaozhen.basic.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.zhaozhen.basic.Constant.LoginConstant;
import org.zhaozhen.basic.businessexception.BusinessException;
import org.zhaozhen.basic.service.IVerifyCodeService;
import org.zhaozhen.basic.util.SmsUtils;
import org.zhaozhen.basic.util.StrUtils;
import org.zhaozhen.user.domain.User;
import org.zhaozhen.user.mapper.UserMapper;

import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class VerifyCodeServiceImpl implements IVerifyCodeService{
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private UserMapper userMapper;

    @Override
    public void sendSmsCode(Map<String,String> param) {
        String phone = param.get("phone");
        String type = param.get("type");// register  binder
        //一:基本校验
        //   1.手机号不能为空
        if(StringUtils.isEmpty(phone) || StringUtils.isEmpty(type)){
            throw new BusinessException("参数不能为空!!");
        }
        String businessKey = "";
        if("register".equals(type)){//注册
            //   2.手机不能被注册
            User user = userMapper.loadByPhone(phone);
            if(user != null){
                throw new BusinessException("手机号已经注册,请直接登录!!!");
            }
            businessKey = LoginConstant.PHONE_REGISTER + phone;// phone_register:1598249
        }else if("binder".equals(type)){//微信绑定
            businessKey = LoginConstant.PHONE_BINDER + phone;// phone_binder:1598249
        }
        sendCode(businessKey);

    }

    //真正发送验证码的逻辑
    public void sendCode(String businessKey){
        //二:校验验证码是否有效
        Object codeTmp = redisTemplate.opsForValue().get(businessKey);
        //      key   verifycode_register:15982495855
        //      value  9527:23469854676424
        //     (根据key从redis中能获取到数据  还没有过3分钟)
        String code = "";
        if(codeTmp != null){//  9527:23469854676424
            // 1.如果拿到值,说明还没有失效
            Long oldSendTime = Long.valueOf(codeTmp.toString().split(":")[1]);
            //   1.1 判断是否过了重发时间(当前时间-23469854676424) <=1 * 60 *1000
            if((System.currentTimeMillis() - oldSendTime.longValue()) <= 1*60*1000){
                //      1.1.1 没过  报错:请勿重复获取验证码
                throw new BusinessException("请勿重复获取验证码!!!");
            }
            //      1.1.2 过了  使用原来的验证码   9527
            code = codeTmp.toString().split(":")[0];
        }else{
            //   2.1 生成一个新的验证过码  8848
            // 2.拿不到 要么就是验证码已经失效,要么就是第一次发送 生成新的
            code = StrUtils.getComplexRandomString(6);
        }
        //三.保存验证码  设置key的过期时间为3分钟
        String codeValue = code + ":"+ System.currentTimeMillis();
        redisTemplate.opsForValue().set(
                businessKey,  //verifycode_register:15982495855
                codeValue,   //验证按 9527:23469854676424
                3,  //3分钟有效期
                TimeUnit.MINUTES);//分钟
        //四.调用发送短信的接口,将验证码,发给客人手机
        //SmsUtils.sendSms(phone,"您的宠物乐园平台的注册验证码是:"+code+",请在3分钟内使用");
        System.out.println("您的宠物乐园平台的注册验证码是:" + code + ",请在3分钟内使用");
    }
}
